(function(){
    var wrapBox = $("#wrap");
    var dpBox = $(".dropDown-box");
    var dpBtn = $("#dropDown-btn");
    var curUrl = window.location.href;
    var arrUrl = new Array();
    var scrollHeight = window.document.body.clientHeight || window.documentElement.clientHeight;
    var tops = 740;

    var leftBox = $(".leftBox");
    var rightBox = $(".rightBox");
    var cont = $("#cont");
    var searchBox = $("#searchBox");
    var header = $("#header");
    var contents = $("#contents");

    function strToNumber(str){
        return parseInt(str.replace(/[^0-9]/ig, ""));
    }

    // var dpBox_padTop = strToNumber(dpBox.css("padding-top"));
    // var dpBox_padBottom = strToNumber(dpBox.css("padding-bottom"));
    // var dpBox_height = dpBox.height() + dpBox_padTop + dpBox_padBottom;

    // var surplusLeft = scrollHeight - header.height() - dpBox_height;
    // var surplusRight = scrollHeight - header.height() - dpBox_height - searchBox.height();
    // (function(){
    //     if(scrollHeight){
    //         leftBox.css("height", surplusLeft +"px");
    //         rightBox.css("height", surplusLeft +"px");
    //         cont.css("height", (surplusRight-20) +"px");
    //         tops = ($("#header").height()+$("#leftMenu").height()+125) - dpBox_height;
    //         $("#wrap").css("top", "-" + tops +"px");
    //     }
    // })();
    

    if(contents.children().length>1){
        cont.css("height",690-contents.children('#searchBox').height()+"px");
    }else{
        cont.css("height","590px");
    }
    // console.log(curUrl.indexOf("#down"));
    if(curUrl.indexOf("#down")>0){
        $("#contShop").hide();
    }else{
        $("#contShop").show();
    }
    if(curUrl.indexOf("#") > 0){
        arrUrl = curUrl.split("#");
        if(arrUrl[1] === "down"){
            openMyBox(dpBtn, dpBox, wrapBox);
        }
    }
    
    dpBtn.live("click",function(){
        if($(this).hasClass("on")){
            closeMyBox($(this), dpBox, wrapBox);
            $("#contShop").show();
        }else{
            openMyBox($(this), dpBox, wrapBox);
            $("#contShop").hide();
        }
    });

    function closeMyBox(){
        arguments[0].removeClass("on");
        arguments[1].css('background','none');
        arguments[2].stop().animate({top:"-"+tops+'px'},{queue:false, duration:500, easeOutQuad: 'easeOutBounce'});
    }

    function openMyBox(){
        arguments[0].addClass("on");
        arguments[1].css('background','url("image/transparent.png") repeat');
        arguments[2].stop().animate({top:'0'},{queue:false, duration:600, easeOutQuad: 'easeOutBounce'});
    }

    $("#cont").jscroll({
        W:"5px",
        Btn:{btn:false},
        Bg:"#000",
        Bar:{
            Pos:"up",
            Bg:{
                Out:"#7b7b7c",
                Hover:"#7b7b7c",
                Focus:"#5b5b5c"
            },
            Bd:{
                Out:"#000",
                Hover:"#000"
            }
        }
    });

    $("#contShop li").dbRotate2D();
    
    
    /*
    // $("#leftMenu ul li").hover(function(){
    //     $(this).siblings().find("a:first").removeClass("over");
    //     $(this).find("a:first").addClass("over");
    //     $(this).siblings().find(".secondMenu").removeClass("cur");
    //     $(this).find(".secondMenu").addClass("cur");
    // });
    */

    //动态导航
    var menuLi = $("#leftMenu .leftBox li");
    var leftBox = $(".leftBox");
    var rightBox = $(".rightBox");
    var subMenu = $(".subMenu");
    var indexCur;

    //查询当前主菜单项，并设置高亮背景
    $.each(menuLi, function(i){
        var t = menuLi.eq(i).find("a.nav");
        if(t.hasClass("cur")){
            indexCur = i;
            t.css("background-position",-t.width()+"px 0");
            // return true;
        }
    });

    if(subMenu.eq(indexCur).find("a").hasClass("over")){
        subMenu.eq(indexCur).show();
    }
    
    //阻止冒泡事件
    function ev(arg){
        var e = window.arg || arg;
        if(e.stopPropagation){
            e.stopPropagation();
        }else{
            e.cancelBubble = true;
        }
    }

    //主菜单hover事件处理
    leftBox.find("li").live("mouseover", function(event){
        var that = $(this);
        var timeOut = setTimeout(function(){
            showMenu(that);
        }, 250);

        rightBox.hover(function(){
            clearTimeout(timeOut);
        }, function(){
            hideMenu(that);
        });
        ev(event);
    }).live("mouseout", function(event){
        var that = $(this);
        var timeOut = setTimeout(function(){
            hideMenu(that);
        },250);
        
        rightBox.hover(function(){
            clearTimeout(timeOut);
        }, function(){
            subMenu.hide(250);
        });
        ev(event);
    });
    
    //显示---主菜单高亮背景
    function showMenu(obj){
        if(obj.index() != indexCur){
            leftBox.find("li").eq(indexCur).find("a.nav").stop().animate({backgroundPosition:"0 0"}, 200);
            obj.find("a.nav").stop().animate({backgroundPosition:"-100px 0"}, 200);
        }else{
            leftBox.find("li").eq(indexCur).find("a.nav").css("background-position", "-100px 0");
        }
        //子菜单显示隐藏
        $(".subMenu").each(function(index){
            if(obj.index() == index){
                $(this).show();
            }else{
                $(this).hide();
            }
        });
    }

    //隐藏---主菜单高亮背景
    function hideMenu(obj){
        // console.log(subMenu.index());
        if((obj.index() != indexCur) || (subMenu.index() != indexCur)){
            leftBox.find("li").eq(indexCur).find("a.nav").stop().animate({backgroundPosition:"-100px 0"}, 200);
            obj.find("a.nav").stop().animate({backgroundPosition:"0 0"}, 200);
        }
        subMenu.eq(obj.index()).hide();
    }



    //选择框插件调用
    if($("#cate").length!=0 || $("#sort").length!=0){
        $("#cate, #sort").select2({
            width:"180px"
        });
    }
})();

//图片替换
$("#cont .ct .m-right .mi ul li").click(function(){
    var originImg = $(this).find("img").attr("origin-img");
    var originUrl = $(this).find("img").attr("origin-url");
    var originTitle = $(this).find("img").attr("origin-title");
    var originTime = $(this).find("img").attr("origin-time");
    $("#cont .ct .m-left .big-pic img").attr("src", originImg);
    $("#cont .ct .m-left p a").attr("href", originUrl);
    $("#cont .ct .m-left p a span.mt_title").html(originTitle);
    $("#cont .ct .m-left p a span.mt_time").html(originTime);
});
